package com.xhj.xmly.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.xhj.xmly.BR;
import com.xhj.xmly.R;
import com.xhj.xmly.adapter.CommentAdapter;
import com.xhj.xmly.bean.ProvinceRadio;
import com.xhj.xmly.bean.ResultBean;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProvinceRadioFragment extends Fragment implements Callback<ProvinceRadio> {

    private int pageNum = 1;
    private ListView listview;
    private List<ResultBean> list;
    private CommentAdapter commentAdapter;

    public ProvinceRadioFragment() {
        // Required empty public constructor
    }

    public static ProvinceRadioFragment newInstance(String id) {

        Bundle args = new Bundle();
        args.putString("id",id);
        ProvinceRadioFragment fragment = new ProvinceRadioFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_province_radio, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listview = ((ListView) view.findViewById(R.id.province_radio_listview));
        TextView emptyTextView = (TextView) view.findViewById(R.id.province_radio_textview);
        listview.setEmptyView(emptyTextView);
        list = new ArrayList<>();
        commentAdapter = new CommentAdapter(getContext(), list, R.layout.broadcast_item2, BR.result);
        listview.setAdapter(commentAdapter);
        String id = getArguments().getString("id");
        new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class)
                .getResult(pageNum,20,id,2).enqueue(this);
    }

    @Override
    public void onResponse(Call<ProvinceRadio> call, Response<ProvinceRadio> response) {
        List<ResultBean> result = response.body().getResult();
        commentAdapter.addAll(result);
    }

    @Override
    public void onFailure(Call<ProvinceRadio> call, Throwable t) {

    }
}
